from django.contrib.auth.models import User	#使用Django自带的User表
from django.db import models
import os

class Sort(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    sort_id = models.AutoField(primary_key=True)
    sort_name = models.CharField(max_length=255)
    def __str__(self):
        return f'{self.sort_name}>{self.user.username}'

class Category(models.Model):
    sort = models.ForeignKey(Sort, on_delete=models.CASCADE)
    category_id = models.AutoField(primary_key=True)
    category_name = models.CharField(max_length=255)
    def __str__(self):
        return f'{self.category_name}({self.sort})'

class Property(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    property_id = models.AutoField(primary_key=True)
    property_name = models.CharField(max_length=255)
    super_property = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)
    remark = models.CharField(max_length=255, null=True, blank=True)
    def __str__(self):
        if self.super_property is None:
            return f'{self.property_name}>{self.category}'
        else:
            return f'{self.super_property.property_name}>{self.property_name}>{self.category}'
        
def get_object_image_path(instance, filename):
    return os.path.join('YYcontainer', instance.sort.user.username, 'object_images', filename)

class Object(models.Model):
    sort = models.ForeignKey(Sort, on_delete=models.CASCADE)
    object_id = models.AutoField(primary_key=True)
    object_name = models.CharField(max_length=255)
    purchase_date = models.DateField()
    price = models.FloatField()
    image = models.ImageField(upload_to=get_object_image_path, null=True, blank=True)
    remark = models.CharField(max_length=255, null=True, blank=True)
    def __str__(self):
        return f'{self.object_name}({self.sort})'

class ObjectProperty(models.Model):
    object = models.ForeignKey(Object, on_delete=models.CASCADE)
    property = models.ForeignKey(Property, on_delete=models.CASCADE)
    def __str__(self):
        return f'{self.object.object_name}-{self.property.property_name}'

def get_suit_image_path(instance, filename):
    return os.path.join('YYcontainer', instance.sort.user.username, 'suit_images', filename)

class Suit(models.Model):
    sort = models.ForeignKey(Sort, on_delete=models.CASCADE)
    suit_id = models.AutoField(primary_key=True)
    suit_name = models.CharField(max_length=255)
    suit_objects = models.ManyToManyField(Object)
    image = models.ImageField(upload_to=get_suit_image_path, null=True, blank=True)
    remark = models.CharField(max_length=255, null=True, blank=True)
    def __str__(self):
        return f'{self.suit_name}({self.sort.user.username})'

class Record(models.Model):
    sort = models.ForeignKey(Sort, on_delete=models.CASCADE)
    date = models.DateField(primary_key=True)
    record_objects = models.ManyToManyField(Object)
    suit = models.ForeignKey(Suit, on_delete=models.CASCADE, null=True, blank=True)
    class Meta:
        ordering = ['-date']
    def __str__(self):
        return f'{self.date}({self.sort.user.username})'